﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace Reviewer.Utilities.Data.MySql
{
    /// <summary>
    /// MySql Column information helper
    /// </summary>
    [CLSCompliant(false)]
    public sealed class MySqlColumnInfo
    {
        #region constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="MySqlColumnInfo"/> class.
        /// </summary>
        private MySqlColumnInfo() { }

        /// <summary>
        /// Initializes a new instance of the <see cref="MySqlColumnInfo"/> class.
        /// </summary>
        /// <param name="columnIndex">Index of the column.</param>
        /// <param name="columnName">Name of the column.</param>
        /// <param name="dataType">Type of the data.</param>
        public MySqlColumnInfo(int columnIndex, string columnName, MySqlDbType dataType) : this(columnIndex, columnName, dataType, 0) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="MySqlColumnInfo"/> class.
        /// </summary>
        /// <param name="columnIndex">Index of the column.</param>
        /// <param name="columnName">Name of the column.</param>
        /// <param name="dataType">Type of the data.</param>
        /// <param name="size">The size.</param>
        public MySqlColumnInfo(int columnIndex, string columnName, MySqlDbType dataType, int size)
        {
            this.ColumnIndex = columnIndex;
            this.Name = columnName;
            this.DataType = dataType;
            this.Size = size;
        }
        #endregion

        #region properties (public)
        /// <summary>
        /// Gets or sets the index of the column.
        /// </summary>
        /// <value>The index of the column.</value>
        public int ColumnIndex { get; set; }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the type of the data.
        /// </summary>
        /// <value>The type of the data.</value>
        public MySqlDbType DataType { get; set; }

        /// <summary>
        /// Gets or sets the size.
        /// </summary>
        /// <value>The size.</value>
        public int Size { get; set; }
        #endregion

        #region methods (public)
        /// <summary>
        /// Adds the parameter.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <param name="value">The value.</param>
        /// <returns>Added parameter</returns>
        public MySqlParameter AddParameter(MySqlCommand command, object value)
        {
            MySqlParameter param;
            switch (this.DataType)
            {
                case MySqlDbType.VarChar:
                    param = command.Parameters.Add("@" + this.Name, this.DataType, this.Size);
                    param.Value = value;
                    break;
                case MySqlDbType.Bit:
                    bool parsedValue = false;
                    if (value is Boolean)
                    {
                        parsedValue = (Boolean)value;
                    }
                    else
                    {
                        if (!Boolean.TryParse(value.ToString(), out parsedValue))
                        {
                            parsedValue = false;
                        }
                    }
                    param = command.Parameters.Add("@" + this.Name, this.DataType, this.Size);
                    param.Value = parsedValue ? 1 : 0;
                    break;
                default:
                    param = command.Parameters.Add("@" + this.Name, this.DataType);
                    param.Value = value;
                    break;
            }
            return param;
        }
        #endregion
    }
}
